﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    using System.Data;
    using Entities;

    public abstract class TaiKhoanDAL : Base, Ibase
    {
        
        public DataTable Select()
        {
            var sql = "SELECT * FROM TAI_KHOAN";
            return ExecuteQuery(sql);
        }

        public int tongso()
        {
            var sql = "select count(TEN_DANG_NHAP) as tong_so from TAI_KHOAN";
            return ExecuteNonQuery(sql);
        }
        public int count()
        {
            return Select().Rows.Count;
        }

        public object Select(string id)
        {
            try
            {
                var sql = "SELECT * FROM TAI_KHOAN WHERE TEN_DANG_NHAP = '{0}'";
                sql = String.Format(sql, id);
                var tmp = ExecuteQuery(sql);

                if (tmp != null & tmp.Rows.Count > 0)
                {
                    var dg = new TaiKhoan();
                    dg.TEN_DANG_NHAP = tmp.Rows[0]["TEN_DANG_NHAP"] + "";
                    dg.MAT_KHAU = tmp.Rows[0]["MAT_KHAU"] + "";
                    dg.QUYEN_TRUY_CAP = tmp.Rows[0]["QUYEN_TRUY_CAP"] + "";
                 
                    return dg;
                }
                else return null;
            }
            catch { return null; }
        }

        public int Update(object obj)
        {
            try
            {
                var o = (TaiKhoan)obj;
                var sql = " UPDATE TAI_KHOAN SET MAT_KHAU = N'{1}', QUYEN_TRUY_CAP = N'{2}' WHERE TEN_DANG_NHAP = '{0}'";
                sql = string.Format(sql, o.TEN_DANG_NHAP, o.MAT_KHAU, o.QUYEN_TRUY_CAP);
                return ExecuteNonQuery(sql);
            }
            catch
            {
                return -1;
            }
        }

        public int Insert(object obj)
        {
            try
            {
                var o = (TaiKhoan)obj;
                var sql = "INSERT INTO TAI_KHOAN(TEN_DANG_NHAP, MAT_KHAU, QUYEN_TRUY_CAP) VALUES ('{0}', N'{1}', N'{2}')";
                sql = string.Format(sql, o.TEN_DANG_NHAP, o.MAT_KHAU, o.QUYEN_TRUY_CAP);
                return ExecuteNonQuery(sql);
            }
            catch
            {
                return -1;
            }
        }

        public int Delete(string id)
        {
            try
            {

                var sql = "DELETE FROM TAI_KHOAN WHERE TEN_DANG_NHAP = '{0}'";
                sql = string.Format(sql, id);
                return ExecuteNonQuery(sql);
            }
            catch
            {
                return -1;
            }
        }
    }
}
